package dao;

import util.PinyinUtil;

import java.io.File;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Objects;

// 这个实例就代表file_meta 表 里的每个记录
public class FileMeta {
    private int id;
    private String name;
    private String path;
    private boolean isDirectory;
    private long size;
    private long lastModified; // 时间戳 转化成合理的时间
//    private String sizeText;
    // 以下两个属性可以没有，可根据name属性来
//    private String pinyin;
//    private String pinyinFirst;

    public String getPinyin() {
        return PinyinUtil.get(name,true);
    }
    public String getPinyinFirst() {
        return PinyinUtil.get(name,false);
    }
    public String getSizeText() {
        // 通过这个方法吧字节换成合理的单位
        // 主要是四个单位 Byte kb mb Gb
        // 如果size小于1024
        // 如果size大于1024 并且小于1024 * 1024 单位升级
        double curSize = size;
        String[] units = {"Byte","KB","MB","GB"};
        for (int level = 0;level < units.length;level++) {
            if(curSize < 1024) {
                // 单位不用升级
                return String.format("%.2f " + units[level],new BigDecimal(curSize));
            }
            curSize /= 1024;
        }
        // 单位升级为GB 还不够，就还是GB就行了
        return String.format("%.2f GB",new BigDecimal(curSize));
    }
    public String getLastModifiedText() {
        // 建议去搜 快去搜！！！
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(lastModified);
    }

    public FileMeta(String name, String path, boolean isDirectory, long size, long lastModified) {
        this.name = name;
        this.path = path;
        this.isDirectory = isDirectory;
        this.size = size;
        this.lastModified = lastModified;
    }

    public FileMeta(File f) {
        this(f.getName(),f.getParent(),f.isDirectory(),f.length(),f.lastModified());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FileMeta fileMeta = (FileMeta) o;
        return isDirectory == fileMeta.isDirectory && Objects.equals(name, fileMeta.name) && Objects.equals(path, fileMeta.path);
    }


    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public boolean isDirectory() {
        return isDirectory;
    }

    public long getSize() {
        return size;
    }

    public long getLastModified() {
        return lastModified;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setDirectory(boolean directory) {
        isDirectory = directory;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public void setLastModified(long lastModified) {
        this.lastModified = lastModified;
    }
}
